package ee.peeter.irc;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.NickAlreadyInUseException;
import org.jibble.pircbot.PircBot;
import org.jibble.pircbot.User;



public class IRCConnection extends PircBot {

	private static IRCConnection irc;
    
	public static IRCConnection getIrcConnection() {
		if(irc == null)
			irc = new IRCConnection();
		return irc;
	}
	public boolean onChannel(String channel) {
		List<String> channels = Arrays.asList(getIrcConnection().getChannels());
		System.out.println(Arrays.toString(getIrcConnection().getChannels()));
		return channels.contains(channel);
	}
    /**
     * Creates a new IRC connection
     */
	private IRCConnection() {
		this.setVerbose(true);
		
        this.setName("IDontCare");
        try {
			this.connect("port80.se.quakenet.org");
		} 
        catch (NickAlreadyInUseException e) {
        	 this.setName("IDontCare"+ (char)Math.round(Math.random()));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (IrcException e) {
			e.printStackTrace();
		}
		

		this.joinChannel("#collapsed.et");

	}
    public void onDisconnect() {

    	try {
			Thread.sleep(20000);
		} 
    	catch (InterruptedException e2) {
			e2.printStackTrace();
		}
    	while (!isConnected()) {
    	    try {
    	        reconnect();
    	    }
    	    catch (Exception e) {
    	    	this.disconnect();
    	    	try {
    				Thread.sleep(20000);
    			}
    	    	catch (InterruptedException e2) {
    				e2.printStackTrace();
    			}
    	    }
    	}
    }
    
    //public Rcon rcon = null;
    @Override
    public void handleLine(String line) {
    	super.handleLine(line);
    	System.out.println("My line: " + line);
    }
    
    
    
    public void onMessage(String channel, String sender, String login, String hostname, String message) {
    	   	
		User user = null;
		for(User u : irc.getUsers(channel)) {
			if(u.getNick().equalsIgnoreCase(sender)) {
				user = u;
				break;
			}
		}
    	irc.sendRawLineViaQueue("whois " + sender);
    	
		
//		User user = null;
//		for(User u : irc.getUsers(channel)) {
//			if(u.getNick().equalsIgnoreCase(sender)) {
//				user = u;
//				break;
//			}
//		}
//		String args[] = message.split(" ");
//		String command = args[0];
//
//		String level = user.getPrefix();
//		if(level.equalsIgnoreCase("@")) {
//			if(command.equalsIgnoreCase("!kick")) {
//       			String s = "";
//       			for(int i = 1; i < args.length;i++) {
//       				s += args[i] + " ";
//       			}
//       			s = s.substring(0, s.length()-1);
//       			try {
//       				int id = Integer.valueOf(s);
//       				rcon.executeRCON("clientkick " + id);
//       			}
//       			catch(Exception e) {
//       				rcon.executeRCON("kick " + s);
//       			}
//       			irc.sendNotice(user.getNick(), "Check !status to make sure your kick worked.");
//       		}
//   			else if(command.equalsIgnoreCase("!ref")) {
//   				String s = "";
//   				for(int i = 1; i < args.length;i++) {
//   					s += args[i] + " ";
//   				}
//   				s = s.substring(0, s.length()-1);
//   				String x = rcon.executeRCON("ref referee " + s);
//   				String y = "ClientUserinfoChanged: " + s;
//   				if(x.contains(y)) {
//   					String line = x.substring(x.indexOf(y)+y.length()+3);
//   					line = line.substring(0, line.indexOf("\\t"));
//   					x = "Refereed \"" + line + "\".";
//   				}
//   				irc.sendNotice(user.getNick(), x);
//   			}
//   			else if(command.equalsIgnoreCase("!unref")) {
//   				String s = "";
//   				for(int i = 1; i < args.length;i++) {
//   					s += args[i] + " ";
//   				}
//   				s = s.substring(0, s.length()-1);
//   				String x = rcon.executeRCON("ref unreferee " + s);
//   				String y = "ClientUserinfoChanged: " + s;
//   				if(x.contains(y)) {
//   					String line = x.substring(x.indexOf(y)+y.length()+3);
//   					line = line.substring(0, line.indexOf("\\t"));
//   					x = "Un-refereed \"" + line + "\".";
//   				}
//   				irc.sendNotice(user.getNick(), x);
//   			}
//       		else if(command.equalsIgnoreCase("!setpass")) {
//       				String s = "";
//       				for(int i = 1; i < args.length;i++) {
//       					s += args[i] + " ";
//       				}
//       				s = s.substring(0, s.length()-1);
//       				if(s.length() < 1 || s.equalsIgnoreCase(" ")) {
//       					irc.sendNotice(user.getNick(), "Pass can't be empty!");
//       					return;
//       				}
//   					String newpw = rcon.executeRCON("g_password " + s);
//   	    			irc.sendNotice(user.getNick(), "Pass set to: " + s + "|" + newpw);
//       			}
//    	}
//		if(level.equalsIgnoreCase("@") || level.equalsIgnoreCase("+")) {
//			if(command.equalsIgnoreCase("!ip")) {
//	      		String returnval = "";
//	      		returnval = rcon.executeRCON("g_password");
//		      	returnval = returnval.substring(returnval.indexOf("\"g_password\" is:\"")+"\"g_password\" is:\"".length());
//		    	returnval = returnval.substring(0,returnval.indexOf("^7\"")); 
//	      		irc.sendMessage(channel, "/connect " + rcon.getIP() + "; password " + returnval);
//	    		}
//			else if(command.equalsIgnoreCase("!info")) {
//				List<Player> players = rcon.getPlayers();
//				irc.sendMessage(channel, "Map: " + rcon.getMap());
//				irc.sendMessage(channel, "Players: " + players.size());
//				if(players.size() > 0) {
//					irc.sendMessage(channel, "-------------------------");
//					for(Player p : players) {
//						irc.sendMessage(channel, "\"" + p.username + "\" slot: " + p.slot);
//					}
//				}
//			}
//
//		}
    }
}
